package com.barradata.app0624.modules.privacy

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.view.View
import com.barradata.R
import com.barradata.app0624.config.Constant
import com.barradata.app0624.data.AppDataManager
import com.barradata.app0624.modules.web.WebActivity
import com.barradata.common.base.BaseActivity
import com.barradata.common.rxbinding.clickV
import com.barradata.common.utils.ToastUtils
import crossoverone.statuslib.StatusUtil
import kotlinx.android.synthetic.main.privacy_activity.*

/**
 * Author:    WuNan
 * Email:     wunan@barradata.com
 * Created:   2019/7/15 18:52
 * Description:
 **/
class PrivacyActivity : BaseActivity() {
    companion object {
        fun actionStart(context: Context) {
            val intent = Intent(context, PrivacyActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun getLayoutId() = R.layout.privacy_activity

    @SuppressLint("SetJavaScriptEnabled")
    override fun initView() {
        StatusUtil.setSystemStatus(this, true, false)

        web_view.settings.javaScriptEnabled = true
        web_view.settings.defaultTextEncodingName = "utf-8"
        web_view.setBackgroundColor(0) // 设置背景色

        web_view.loadUrl(Constant.PRIVACY_AGREEMENT_URL)
        web_view.visibility = View.VISIBLE
        next_button.clickV().filter { isReadAgreement() }.subscribe {
            AppDataManager.hasOpened = true
            WebActivity.actionStart(this)
            finish()
        }.autoClean()
    }

    private fun isReadAgreement(): Boolean {
        if (!checkbox.isChecked) {
            ToastUtils.showShort(R.string.privacy_agreement_not_read)
            return false
        }
        return true
    }
}